package com.gsoe.auth.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class EmailUtils {
    @Value("${email.sendEmail}")
    private String sender;
    @Value("${email.nickname}")
    private String nickname;
    @Autowired(required=false)
    private JavaMailSender javaMailSender;

    public void sendToEmail(String email,String message,String subject) {
        // 创建一个邮件
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        // 设置发件人
        simpleMailMessage.setFrom(nickname+'<'+sender+'>');

        // 设置收件人
        simpleMailMessage.setTo(email);

        // 设置邮件主题
        simpleMailMessage.setSubject(subject);

        simpleMailMessage.setText(message);


        javaMailSender.send(simpleMailMessage);

    }
}
